package config

import (
	"fmt"
	"gitee.com/lanshiren/miniprogram/app/log"
	"github.com/BurntSushi/toml"
	"github.com/jinzhu/gorm"
)

// Conf ...
var (
	Conf      *Config
	defaultDB *gorm.DB
)

// Config ...
type Config struct {
	Listen       string   `toml:"listen"`
	SaltMinions  []string `toml:"salt_minions"`
	SaltAPI      string   `toml:"salt_api"`
	SaltUsername string   `toml:"salt_username"`
	SaltPassword string   `toml:"salt_password"`
	RedisURL     string   `toml:"redis_url"`
	NginxLogPath string   `toml:"nginx_log_path"`
	MysqlUrl     string   `toml:"mysql_url"`
	AppID        string   `toml:"app_id"`
	Secret       string   `toml:"secret"`
	AccessKey    string   `toml:"access_key"`
}

// GetDB ...
func GetDB() *gorm.DB {
	return defaultDB
}

// SetDB ...
func SetDB(db *gorm.DB) {
	defaultDB = db
}

// LoadConfig ...
func LoadConfig(path string) (*Config, error) {
	var c Config
	if _, err := toml.DecodeFile(path, &c); err != nil {
		log.MainLogger.Error(fmt.Sprintf("config file parse failed, %v", err))
		return nil, err
	}
	Conf = &c
	return &c, nil
}
